package org.example;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


//集合遍历
public class TextIterator {
    public static void demoFor() {
        List<String> list = List.of("aa", "bb", "cc", "dd", "ee");
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
    }

    public static void demoForEach() {
        List<String> list = List.of("qq", "ww", "uu", "rr", "tt");
        for (String s : list) {
            s = "111";
        }
        System.out.println(list);
        List<String> list1 = new ArrayList<>();
        list1.add("ll");
        list1.add("cv");
        int index = 0;
        for (String s : list1) {
            s = s+"aaa";
        }
        System.out.println(list1);
    }

    public static void demoIterator() {
        List<String> list = List.of("作者", "得到", "调查");
        //返回Iterator对象 遍历输出
        Iterator<String> it = list.iterator();
        while (it.hasNext()){ //hasNext判断集合中是否有下个元素，如果有返回true
            String next = it.next();// 获取（下一个)元素
            System.out.println(next);
        }
        List<String> list2 = new ArrayList<>();
        list2.add("实现");
        list2.add("是多");
        list2.add("哦哦");
        list2.add("但是");
        //list2.remove(1)和iterator.next()不能同时使用
//        Iterator<String> iterator = list2.iterator();
//        while (iterator.hasNext()){
//            if (list2.get(1).equals("哦哦")){
//                list2.remove(1);
//            }
//            System.out.println(iterator.next());
//        }
        //如果list2内有哦哦就把索引1删除
        for (int i = 0; i < list2.size(); i++) {
            if(list2.get(i).equals("哦哦")){
                list2.remove(1);
            }
        }
        System.out.println(list2);



}

    public static void main(String[] args) {
        demoFor();
//        demoForEach();
//        demoIterator();
    }
}
